package ua.in.iticket.addressbook.person;

import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by ivanov-av
 * 05.02.14 15:31.
 */
public class Person implements Comparable{
    protected int pid = -1; // Person id
    protected int age; // возраст
    protected String fullName; // ФИО
    protected List<PhoneNumber> phoneNumbers = new LinkedList<PhoneNumber>(); // телефон
    protected Address address; // адрес

    public Person(int age, String fullName, List<PhoneNumber> phoneNumbers, Address address) {
        this.age = age;
        this.fullName = fullName;
        this.phoneNumbers = phoneNumbers;
        this.address = address;
    }

    public Person() { }


    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    // добавить в список еще один тел.номер
    public void addPhone(PhoneNumber newNumber){
        phoneNumbers.add(newNumber);
    }

    // выдать размер списка тел.номеров
    public int getPhoneNumbersSize(){
        return phoneNumbers.size();
    }

    // выдать номер телефона по индексу из списка
    public PhoneNumber getPhoneNumber(int index) throws IndexOutOfBoundsException{
        if (index > (phoneNumbers.size() - 1)) { throw new IndexOutOfBoundsException("Error: index out of bounds in method getPhoneNumber class Person "); }
        return phoneNumbers.get(index);
     }

    // заменить номер телефона по индексу из списка
    public PhoneNumber setPhoneNumber(int index, PhoneNumber newNumber ) throws IndexOutOfBoundsException{
        if (index > (phoneNumbers.size() - 1)) { throw new IndexOutOfBoundsException("Error: index out of bounds in method getPhoneNumber class Person "); }
        return phoneNumbers.set(index, newNumber);
    }

    // удалить номер телефона по индексу из списка
    public PhoneNumber removePhoneNumber (int index) throws IndexOutOfBoundsException{
        if (index > (phoneNumbers.size() - 1)) { throw new IndexOutOfBoundsException("Error: index out of bounds in method removePhoneNumber class Person"); }
        return phoneNumbers.remove(index);
     }

    // // удалить все номера телефонов
    public void removeAllPhoneNumber(){
        phoneNumbers.clear();
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("age", age)
                .append("fullName", fullName)
                .append("phoneNumbers", phoneNumbers)
                .append("address", address)
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;

        Person person = (Person) o;

        if (age != person.age) return false;
        if (address != null ? !address.equals(person.address) : person.address != null) return false;
        if (fullName != null ? !fullName.equals(person.fullName) : person.fullName != null) return false;
        //if (phoneNumbers != null ? !phoneNumbers.equals(person.phoneNumbers) : person.phoneNumbers != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = age;
        result = 31 * result + (fullName != null ? fullName.hashCode() : 0);
        //result = 31 * result + (phoneNumbers != null ? phoneNumbers.hashCode() : 0);
        result = 31 * result + (address != null ? address.hashCode() : 0);
        return result;
    }

    public int getAge() {
        return age;
    }

    public String getFullName() {
        return fullName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    @Override
    public int compareTo(Object o) { // вызов Collections.sort(listOfPerson);
        Person p=(Person) o;
        int i = this.fullName.toUpperCase().compareTo(p.fullName.toUpperCase());
        if (i == 0) {
            i = this.age - p.age;
        }
        return i;
    }
           
    // public List<PhoneNumber> getPhoneNumbers() {
    //    return phoneNumbers;
    //}


}
